#include"../mqserver/mq_virtualhost.hpp"
#include<gtest/gtest.h>
bitmq::VirtualHost::ptr _vhost;
class HostTest:public testing::Environment
{
    public:
    
    public:
    void SetUp()override
    {
        _vhost=std::make_shared<bitmq::VirtualHost>("./data/host1/message/","./data/host1/host1.db");

        auto msg_map=std::unordered_map<std::string,std::string>();
        _vhost->declareExchange("exchage1",bitmq::ExchageType::DERICT,true,false,msg_map);
        _vhost->declareExchange("exchage2",bitmq::ExchageType::DERICT,true,false,msg_map);
        _vhost->declareExchange("exchage3",bitmq::ExchageType::DERICT,true,false,msg_map);
       
        
        _vhost->declareQueue("queue1",bitmq::ExchageType::DERICT,false,false,msg_map);
        _vhost->declareQueue("queue2",bitmq::ExchageType::DERICT,false,false,msg_map);
        _vhost->declareQueue("queue3",bitmq::ExchageType::DERICT,false,false,msg_map);

        _vhost->bind("exchage1","queue1","news.music#");
        _vhost->bind("exchage1","queue2","news.music#");
        _vhost->bind("exchage1","queue3","news.music#");

        _vhost->bind("exchage2","queue1","news.music#");
        _vhost->bind("exchage2","queue2","news.music#");
        _vhost->bind("exchage2","queue3","news.music#");

        _vhost->bind("exchage3","queue1","news.music#");
        _vhost->bind("exchage3","queue2","news.music#");
        _vhost->bind("exchage3","queue3","news.music#");
        
        _vhost->basicPublic("queue1",nullptr,"hello world1");
        _vhost->basicPublic("queue1",nullptr,"hello world2");
        _vhost->basicPublic("queue1",nullptr,"hello world3");
    }
    void TearDown()override
    {
        _vhost->clear();
    }
   
};
TEST(host_test,insert_test)
{
    ASSERT_EQ(_vhost->existsExchange("exchage1"),true);
    ASSERT_EQ(_vhost->existsExchange("exchage2"),true);
    ASSERT_EQ(_vhost->existsExchange("exchage3"),true);
    
    ASSERT_EQ(_vhost->existsQueue("queue1"),true);
    ASSERT_EQ(_vhost->existsQueue("queue2"),true);
    ASSERT_EQ(_vhost->existsQueue("queue3"),true);

    ASSERT_EQ(_vhost->existsBindings("exchage1","queue1"),true);
    ASSERT_EQ(_vhost->existsBindings("exchage1","queue2"),true);
    ASSERT_EQ(_vhost->existsBindings("exchage1","queue3"),true);

    bitmq::MessagePtr msgp=_vhost->basicConsume("queue1");
    ASSERT_EQ(msgp->payload().body(),"hello world1");
  
    _vhost->deleteExchange("exchage1");
    ASSERT_EQ(_vhost->existsBindings("exchange1", "queue1"), false);
    ASSERT_EQ(_vhost->existsBindings("exchange1", "queue2"), false);
    ASSERT_EQ(_vhost->existsBindings("exchange1", "queue3"), false);

}
int main(int argc,char*argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new HostTest);
    RUN_ALL_TESTS();
    return 0;
}
